//
//  FeedbackViewController.m
//  Runone
//
//  Created by runone02 on 16/9/8.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "FeedbackViewController.h"
#import "MJExtension.h"
#import "ParModel.h"
#import "SysCommentRecordInfoModel.h"
#import "RequestHelper.h"
#import "SVProgressHUD.h"
#import "UITextView+ToolBar.h"
#import "ROTextView.h"
#import "OneConst.h"

@interface FeedbackViewController ()<UITextViewDelegate,UITextFieldDelegate>
@property(nonatomic,strong)SysCommentRecordInfoModel *model;
@property(nonatomic,strong)ParModel *parModel;

@property (weak, nonatomic) IBOutlet UITextField *contactNO;
@property (weak, nonatomic) IBOutlet ROTextView *Guest;
@property(nonatomic,strong)UILabel *label;

@end

@implementation FeedbackViewController

#pragma     ---------意见反馈

- (void)viewDidLoad {
    [super viewDidLoad];
    _label                = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    _label.text                    = @"意见反馈";
    _label.textAlignment           = NSTextAlignmentCenter;
    _label.textColor               = [UIColor whiteColor];
    self.navigationItem.titleView = _label;
    
    _Guest.delegate         = self;
    _Guest.placeholder      = @"请输入您的反馈意见，300字以内";
    _Guest.placeholderColor = [UIColor lightGrayColor];
    _model=[[SysCommentRecordInfoModel alloc] init];
    _parModel=[[ParModel alloc] init];
    [_Guest setTextFieldInputAccessoryView:_Guest frame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    _contactNO.delegate = self;
}

- (void)textViewDidChange:(UITextView *)textView {
    if ( textView.hasText) {
        [_Guest  textDidChange];
    }else {
        [_Guest showPlaceHolder];
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    [self isMobileNumber:textField.text];
    return YES;
}

- (IBAction)confirmBtnClick:(id)sender {
    if (_Guest.text.length>1&&_Guest.text.length<10) {
        [SVProgressHUD showErrorWithStatus:@"反馈长度不能小于10个字符"];
        return;
    }
    if(_Guest.text.length<=0) {
        [SVProgressHUD showErrorWithStatus:@"反馈内容不能为空"];
        return;
    }
    if (![self isMobileNumber:_contactNO.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码"];
        return;
    }else {
        [self request];
    }
}

-(void)textViewDidBeginEditing:(UITextView *)textView {
//    if ([_Guest.text isEqualToString:@"请输入您的反馈意见，300字以内"]) {
//        _Guest.text=@"";
//        _Guest.textColor=[UIColor blackColor];
//    }
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length<1||textView.text.length==0) {
        return;
    }
    if(textView.text.length > 300){
        [SVProgressHUD showErrorWithStatus:@"字数不能超过300"];
    }
}

//判断电话号码是否合法
- (BOOL)isMobileNumber:(NSString *)mobileNum {
    if (mobileNum.length != 11) {
        return NO;
    }
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\\d{8}$";
    NSString *CM     = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";
    NSString *CU     = @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";
    NSString *CT     = @"(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm     = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu     = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct     = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum]  == YES)
        || ([regextestct evaluateWithObject:mobileNum]  == YES)
        || ([regextestcu evaluateWithObject:mobileNum]  == YES)) {
        return YES;
    }
    else {
        return NO;
    }
}

//刷新数据
-(void)request {
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetCurrentDutyPersonalName;
    getCurrentEventModel.DataValue = [self jsonstring];
    getCurrentEventModel.UpdatedTime=@"2012-01-12 12:00:00";
    getCurrentEventModel.SystemCode=@"GuangleHighway";
    getCurrentEventModel.MethodName=@"SaveSysCommentRecordInfo";
    
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        if([requestOutginseng.Code isEqualToString:@"001"])
        {
            _label.text=@"";
            _Guest.text=@"";
            _contactNO.text=@"";
            
            [SVProgressHUD showSuccessWithStatus:@"反馈成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [SVProgressHUD showSuccessWithStatus:@"反馈失败"];
            return;
        }
    };
    
    [requestHelper RequestGinsengModel:getCurrentEventModel andPushController:self];
}

-(void)viewWillDisappear:(BOOL)animated
{
    _label.text=@"";
    _Guest.text=@"";
    _contactNO.text=@"";
}

-(NSString *)jsonstring {
    _model.Content = _Guest.text;
    _model.Contact = _contactNO.text;
    NSMutableDictionary *dic=_model.mj_keyValues;
    NSString *temp=[dic mj_JSONString];
    _parModel.key=@"SysCommentRecordInfo";
    _parModel.value=temp;
    
    NSMutableDictionary *dicc=_parModel.mj_keyValues;
    NSString *dateValue=[dicc mj_JSONString];
    NSString *str=@"[";
    NSString *tempp=[str stringByAppendingString:dateValue];
    NSString *str1=@"]";
    NSString *JsonString=[tempp stringByAppendingString:str1];
    return JsonString;
}

@end
